package com.zpark.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zpark.pojo.Person;
import com.zpark.sentinel_block_handler.GlobalHandler;
import com.zpark.utils.ResponseData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @PackageName: com.zpark.controller
 * @ClassName: TestController
 * @Description:
 * @author: RZS
 * @date: 2024/5/7  9:37
 */
@RestController
public class TestController {

    @GetMapping("/t1")
    // 通过注解定义资源
    @SentinelResource(value = "test-01",
    blockHandler = "test01Exception")
    public ResponseData<Person> test01(){
        Person person = new Person();
        person.setId(1001);
        person.setUsername("大锤");
        person.setPassword("123456");

        return new ResponseData<>(2000, "SUCCESS", person);
    }

    @GetMapping("/t2")
    // 通过注解定义资源
    @SentinelResource(value = "test-02",
            blockHandler = "test02Exception",
            blockHandlerClass = GlobalHandler.class)
    public ResponseData<Person> test02(){
        Person person = new Person();
        person.setId(9999);
        person.setUsername("大锤9999");
        person.setPassword("123456");

        return new ResponseData<>(2000, "SUCCESS", person);
    }

    public ResponseData<String> test01Exception(BlockException e){

        return new ResponseData<>(2000, "ERROR", null);
    }
}
